using System;
using System.Collections.Generic;
using System.Text;
using Umdf.SideShow.Events;
using System.Net;
using System.Collections.ObjectModel;

namespace Umdf.SideShow.PocketPC
{
    public class ClientApplicationManager : IPocketPCChannel, ICallbackChannel
    {
        private SideShowApplicationCollection _applications;
        private SideShowApplication _currentApplication;
        private PocketPCClientChannel _channel;
        private Guid _endpointId;

        public event EventHandler ListChanged;
        
        public ClientApplicationManager(Guid endpointId)
        {
            _channel = new PocketPCClientChannel(this);
            _endpointId = endpointId;
        }

        public void Connect(IPAddress host, int port)
        {
            _channel.Open(host, port);
        }

        public void Close()
        {
            _channel.Close();
        }

        public SideShowApplicationCollection Applications
        {
            get
            {
                if (_applications == null)
                {
                    _applications = new SideShowApplicationCollection();
                    _applications.ListChanged += new EventHandler(OnListChanged);
                }
                return _applications;
            }
        }

        private void OnListChanged(object sender, EventArgs e)
        {
            if (ListChanged != null)
            {
                ListChanged(this, e);
            }
        }

        public SideShowApplication CurrentApplication
        {
            get { return _currentApplication; }
            set 
            {
                if (_currentApplication != value)
                {
                    if (_currentApplication != null)
                    {
                        ApplicationExitEvent e = new ApplicationExitEvent(_currentApplication.Id, _endpointId);
                        _channel.PostEvent(e);
                    }
                    _currentApplication = value;
                    if (_currentApplication != null)
                    {
                        ApplicationEnterEvent e = new ApplicationEnterEvent(_currentApplication.Id, _endpointId);
                        _channel.PostEvent(e);
                    }

                }
            }
        }

        
        #region IPocketPCChannel Members

        void IPocketPCChannel.AddApplication(SideShowApplication app)
        {
            Applications.Add(app);
        }

        void IPocketPCChannel.AddContent(Guid applicationId, Guid sideShowEndpointId, Umdf.SideShow.Contents.Content newContent)
        {
            Applications[applicationId].Endpoints[sideShowEndpointId].AddContent(newContent);
        }

        void IPocketPCChannel.RemoveApplication(Guid appId)
        {
            Applications.Remove(appId);
        }

        void IPocketPCChannel.RemoveContent(Guid applicationId, Guid sideShowEndpointId, uint removedContentId)
        {
            Applications[applicationId].Endpoints[sideShowEndpointId].RemoveContent(removedContentId);
        }

        void IPocketPCChannel.Initialize()
        {
            
        }

        void IPocketPCChannel.Deinitialize()
        {
            
        }

        #endregion

        #region ICallbackChannel Members

        void ICallbackChannel.PostEvent(SideShowEvent e)
        {
            _channel.PostEvent(e);
        }

        #endregion
    }
}
